/*
 * @Author: LVGRAPE
 * @Date: 2023-11-08 17:31:04
 * @LastEditTime: 2023-11-15 11:42:08
 * @LastEditors: LVGRAPE
 * @Description:
 * @FilePath: \zino_fc_v0\zino\main.c
 * 要啥没啥，爱咋咋的
 */
#include "at32f4xx.h"
#include "soft_i2c.h"
#include "os_timer.h"
#include "delay.h"
#include  "zino.h"
#include "uart.h"
#include "pwr.h"
extern  void setup(void);
extern  void loop(void);

void setup(void) __attribute((weak));
void loop(void) __attribute((weak));

void setup(void){

}
void loop(void)
{

}

static os_timer_t arduino_timer;;

void main(void)
{
    SystemInit();
	SCB->VTOR = FLASH_BASE | 0x1800;
    SysTick_Init(SystemCoreClock/1000);/*Systick clock 150MHz, 1ms*/

    RCC->APB2EN |= RCC_APB2PERIPH_AFIO;	  // AFIO,IO重映射时钟先打开
	RCC_ADCCLKConfig(RCC_APB2CLK_Div16);
	RCC->APB2EN |= RCC_APB2PERIPH_ADC1;
	RCC->AHBEN |= RCC_AHBPERIPH_DMA1;
	RCC->AHBEN |= RCC_AHBPERIPH_DMA2;
	RCC->APB2EN |= RCC_APB2PERIPH_GPIOA;
	RCC->APB2EN |= RCC_APB2PERIPH_GPIOB;
	RCC->APB2EN |= RCC_APB2PERIPH_GPIOC;
	RCC->APB2EN |= RCC_APB2PERIPH_GPIOD;
	RCC->APB2EN |= RCC_APB2PERIPH_GPIOF;
	RCC->APB2EN |= RCC_APB2PERIPH_USART1; //主调试口
	RCC->APB1EN |= RCC_APB1PERIPH_SPI2; //NRF SPI2
	RCC->APB1EN |= RCC_APB1PERIPH_TMR3; //主电机正转
	RCC->APB1EN |= RCC_APB1PERIPH_TMR4; //主电机反转

    // Free PA13,PA14,PA15,PB3,PB4 from swj
	GPIO_PinsRemapConfig(GPIO_Remap_SWJ_AllDisable, ENABLE);
    
    zino_pin_mode_plus(GPIOA,GPIO_Pins_9, GPIO_Mode_AF_PP);
    zino_pin_mode_plus(GPIOA,GPIO_Pins_10, GPIO_Mode_IN_FLOATING);

    Serial_Begin(&serialHandle[serial1], 115200);

    os_timer_routine_init();

    ex_5v_ctrl(ON);
    
    setup();

    os_timer_init(&arduino_timer, (os_timer_func_t)loop, 0);
    os_timer_start(&arduino_timer, 1, 1);

    zino_board_init();
    zino_components_init();

    while(1)
    {
        os_timer_routine();
    }
}
